/**
 * 全局模块
 *** 下载浮层
 *** 登录浮层
 *** 注册浮层
 **/

 $(function(){
 	window.globalComponent = {};
 	// 创建遮罩层
 	$maskEl = $('<div class="mask"></div>').hide().appendTo('body');

 	globalComponent.download = {
 		show : function(){
 			if(this.inited){
 				$maskEl.show();
 				this.el.show();
 			}else{
 				var me = this;
 				$.get(PINER.root + '/?m=download&a=index').done(function(html){
 					me.el = $(html).appendTo('body');
 					$maskEl.show();
 					me.el.show();

 					// 绑定隐藏事件
 					me.el.bind('click', '.j-close', $.proxy(me.hide, me));
 					me.inited = true;
 				})
 			}
 		},
 		hide : function(ev){
 			ev && ev.preventDefault();
 			if(!this.inited) return;
 			$maskEl.hide();
 			this.el.hide();
 		},
 		init : function(selector){
 			var me = this;
 			$(selector).bind('click', function(ev){
 				ev.preventDefault();
 				me.show();
 			});
 		}
 	};

 	globalComponent.download.init('.j-download');


 	// 全局登录组件 依赖maskEl
 	globalComponent.login = {
 		show : function(ev){
 			ev && ev.preventDefault();
 			// 隐藏互斥组件
 			globalComponent.signup.hide();
 			if(this.inited){
 				$maskEl.show();
 				this.el.show();
 			}else{
 				var me = this;
 				$.get('http://localhost:5555/assets/data/login-fragment.html').done(function(html){
 					me.el = $(html).appendTo('body');
 					$maskEl.show();
 					me.el
 						.bind('click', '.j-close', $.proxy(me.hide, me))
 						.bind('click', '.j-signup', function(ev){
 							ev.preventDefault();
 							me.hide();
 							globalComponent.signup.show();
 						});
 					me.inited = true;

 				})
 			}
 		},
 		hide : function(ev){
 			ev && ev.preventDefault();
 			if(!this.inited) return;
 			$maskEl.hide();
 			this.el.hide();
 		},
 		init : function(selector){
 			$(selector).bind('click', $.proxy(this.show, this));
 		}
 	};

 	globalComponent.login.init('.j-login');


 	// 全局注册组件 依赖maskEl
 	globalComponent.signup = {
 		show : function(ev){
 			ev && ev.preventDefault();
 			if(this.inited){
 				$maskEl.show();
 				this.el.show();
 			}else{
 				var me = this;
 				$.getJSON(PINER.root + '/?m=user&a=register', function(result){
					if(result.status == 0){
						$.pinphp.tip({content:result.msg, icon:'error'});
					}else{
						$.dialog({id:'register', title:'', content:result.data, padding:'', fixed:false, lock:true});
						$.pinphp.dialog.dregister_form($('#J_dregister_form'));
					}
				});
 			}
 		},
 		hide : function(ev){
 			ev && ev.preventDefault();
 			if(!this.inited) return;
 			$maskEl.hide();
 			this.el.hide();
 		},
 		init : function(selector){
 			$(selector).bind('click', $.proxy(this.show, this));
 			
 		}
 	};

 	globalComponent.signup.init('.j-signup');

 });

 function verify_tele(){
	var tele = $('#regRele').val();
	
	var patrn= /^0{0,1}(13[0-9]|145|15[7-9]|153|156|18[0-9])[0-9]{8}$/i;
	
	if (!patrn.test(tele)){ 
		$('#J_dregister_form').find('#J_register_fail').html('手机号格式不对！').css("visibility", 'visible');
	}else{
		$('#J_dregister_form').find('#J_register_fail').html('').css("visibility", 'visible');
	}
 }